using System;
using System.Collections.Generic;
using System.Text;

namespace CallerID
{
    public class NumberFormat
    {
        public static char DigitChar = '#';
        private string m_StartsWith = "";
        private string m_Format = "";
        private int m_Digits = -1;

        public string StartsWith
        {
            get { return m_StartsWith ?? ""; }
            set { m_StartsWith = GetCleanNumber(value); }
        }
        public string Format
        {
            get { return m_Format ?? ""; }
            set { m_Format = GetCleanFormat(value); m_Digits = -1; }
        }

        public int Digits
        {
            get
            {
                if(m_Digits < 0) {
                    m_Digits = 0;
                    if(!string.IsNullOrEmpty(m_Format)) {
                        for(int i = 0; i < m_Format.Length; i++) {
                            if(IsDigitChar(m_Format[i])) m_Digits++;
                        }
                    }
                }
                return m_Digits;
            }
        }

        public NumberFormat()
        {
        }

        public NumberFormat(string startsWith, string format)
        {
            this.StartsWith = startsWith;
            this.Format = format;
        }

        public string FormatNumber(string number)
        {
            if(!string.IsNullOrEmpty(number) && !string.IsNullOrEmpty(m_Format)) {
                StringBuilder sb = new StringBuilder(m_Format.Length);
                int n = 0;
                for(int i = 0; i < m_Format.Length; i++) {
                    if(IsDigitChar(m_Format[i]) && n < number.Length && Char.IsDigit(number[n])) {
                        sb.Append(number[n]);
                        n++;
                    } else {
                        sb.Append(m_Format[i]);
                    }
                }
                return sb.ToString();
            }
            return number;
        }

        public bool Equals(NumberFormat p)
        {
            if(p != null) {
                return this.StartsWith == p.StartsWith && this.Format == p.Format;
            }
            return false;
        }


        private static bool IsDigitChar(char ch)
        {
            return (ch != ' ' && ch != '/' && ch != '-');
        }

        /// <summary>
        /// Returns proper format string, composited of only whitespace, /, - and DigitChar (#).
        /// </summary>
        public static string GetCleanFormat(string format)
        {
            if(!string.IsNullOrEmpty(format)) {
                StringBuilder sb = new StringBuilder(format.Length);
                for(int i = 0; i < format.Length; i++) {
                    char ch = format[i];
                    sb.Append(IsDigitChar(ch) ? NumberFormat.DigitChar : ch);
                }
                return sb.ToString();
            }
            return "";
        }

        /// <summary>
        /// Returns only digits from specified number string.
        /// </summary>
        public static string GetCleanNumber(string number)
        {
            if(!string.IsNullOrEmpty(number)) {
                StringBuilder sb = new StringBuilder(number.Length);
                for(int i = 0; i < number.Length; i++) {
                    char ch = number[i];
                    if(Char.IsDigit(ch)) sb.Append(ch);
                }
                return sb.ToString();
            }
            return "";
        }
    }
}
